/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatGUI;

import Model.User;
import chatNI.ChatNI;
import chatNI.TCPCom;
import common.Message;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Dimension;


import java.io.File;

import java.util.Calendar;
import javax.sound.midi.SysexMessage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author anthony
 */
public class ChatWindow extends JFrame implements ActionListener {

    private JTextArea userChatArea, userInputArea;
    private JScrollPane userChatPane, userInputPane;
    private JButton userSendButton, userOpenButton;
    private JFrame chatFrame;
    private JLabel userInfoLabel;
    private JPanel chatWindow;
    private JFileChooser userFileChooser;
    int userResult;
    File userSelectedFile;
    String output, userFormattedText;
    JTextField usernameField;
    User u;
    ChatController.ChatController cc;

    public JTextArea getUserChatArea() {
        return userChatArea;
    }

    public User getU() {
        return u;
    }

    public ChatWindow(User u, ChatController.ChatController cc) {
        this.u = u;
        this.cc = cc;
        initComp();
    }

    private void initComp() {
        userChatArea = new JTextArea();
        userChatArea.setEditable(false);
        userChatArea.setLineWrap(true);
        userChatPane = new JScrollPane(userChatArea);
        userChatPane.setBounds(38, 32, 467 - 38, 389 - 32);
        userChatPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        userInputArea = new JTextArea();
        userInputArea.setLineWrap(true);
        userInputPane = new JScrollPane(userInputArea);
        userInputPane.setBounds(37, 415, 351 - 37, 477 - 426);
        userInputPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        userSendButton = new JButton("Send");
        userSendButton.addActionListener(this);
        userSendButton.setBounds(365, 400, 462 - 350, 436 - 400);
        userOpenButton = new JButton("Open");
        userOpenButton.setBounds(365, 444, 462 - 350, 479 - 444);
        userOpenButton.addActionListener(this);
        userSendButton.setBackground(Color.YELLOW);
        userSendButton.addActionListener(this);
        chatFrame = new JFrame("Chat with " + u.toString());
        userInfoLabel = new JLabel();
        userInfoLabel.setBounds(38, 396, 352 - 38, 423 - 400);
        chatWindow = new JPanel();
        chatWindow.setBackground(Color.GRAY);
        chatWindow.setLayout(null);
        chatWindow.add(userChatPane);
        chatWindow.add(userInputPane);
        chatWindow.add(userSendButton);
        chatWindow.add(userOpenButton);
        chatWindow.add(userInfoLabel);
        chatFrame.setSize(new Dimension(500, 530));
        chatFrame.setResizable(false);
        WindowListener listener = new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                cc.del_conv(u.getIp());
            }
        };
        addWindowListener(listener);
        revalidate();
        chatFrame.add(chatWindow);
        chatFrame.setLocationRelativeTo(null);
        chatFrame.setIconImage(new ImageIcon("Library/msn_logo.png").getImage());
        chatFrame.setVisible(true);


    }

    public void actionPerformed(ActionEvent e) {



        if (e.getSource() == userOpenButton) {
            File fic =getUserFile();
            try {
                ChatNI.sendTransfertfilereq(fic.getName(), (int) fic.getTotalSpace(), u.getIp());
                TCPCom envoi = new TCPCom(5555, fic.getAbsolutePath());
               
            } catch (Exception err) {
                System.err.println(err);
            }
        }

            if (e.getSource() == userSendButton) {

                userFormattedText = userInputArea.getText().trim();
                if (userFormattedText.isEmpty() == false) {
                    userChatArea.append(" You " + "say :\n" + " " + userFormattedText + "\n");
                    try {
                        ChatNI.sendMsg(userFormattedText, u.getIp());
                    } catch (Exception er) {
                        System.err.println(er);
                    }
                }

                userInputArea.setText(null);
            }

        }
    

    public File getUserFile() {

        userFileChooser = new JFileChooser();
        userFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        userResult = userFileChooser.showOpenDialog(this);
        if (userResult == JFileChooser.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(this, "No file was chosen");
            return null;
        }
        userSelectedFile = userFileChooser.getSelectedFile();
        if (userSelectedFile.exists()) {
            output = userSelectedFile.getName() + " is sending..";
            userInfoLabel.setText(output);

            return userSelectedFile;
        } else {
            JOptionPane.showMessageDialog(this, userSelectedFile + " does not exist");
        }
        return null;
    }
}
